package com.example.webmodule.listener;

import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.example.webmodule.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName EY-NacosListener
 * @Description com.example.webmodule.listener
 * @Author menyj
 * @Date 2025/3/19
 **/
@Slf4j
public class NacosListener implements EventListener {

    private String serviceId;
    private String group;

    public NacosListener(String serviceId, String group) {
        this.serviceId = serviceId;
        this.group  = group;
    }

    @Override
    public void onEvent(Event event) {
        NamingEvent namingEvent = (NamingEvent) event;
        List<Instance> instances = namingEvent.getInstances();
        if (!"multi-platform-module".equals(namingEvent.getServiceName())) {
            // 发布自定义事件
            SpringContextUtil.publishEvent(
                    new InstanceEvent(this, this.serviceId, this.group, instances));
        }
    }
}
